export interface AudioEffectConfig {
  id: string
  name: string
  description: string
  setup: (context: AudioContext) => {
    nodes: Record<string, AudioNode>
    connections: Array<[AudioNode, AudioNode, number?]>
  }
}

export const audioEffects: AudioEffectConfig[] = [
  {
    id: 'normal',
    name: '原声',
    description: '保持音乐原有的音质效果',
    setup: () => ({
      nodes: {},
      connections: []
    })
  },
  {
    id: 'stereo',
    name: '立体环绕',
    description: '增强空间感和声场宽度',
    setup: (context: AudioContext) => {
      const stereoPanner = context.createStereoPanner()
      const delayLeft = context.createDelay()
      const delayRight = context.createDelay()
      const convolver = context.createConvolver()
      const merger = context.createChannelMerger(2)
      const splitter = context.createChannelSplitter(2)

      // 优化延迟时间，使声场更自然
      delayLeft.delayTime.value = 0.025  // 减小延迟时间
      delayRight.delayTime.value = 0.015

      // 优化混响效果
      const reverbTime = 1.5  // 减少混响时间
      const length = context.sampleRate * reverbTime
      const impulse = context.createBuffer(2, length, context.sampleRate)
      for (let channel = 0; channel < 2; channel++) {
        const data = impulse.getChannelData(channel)
        for (let i = 0; i < length; i++) {
          // 使用更自然的衰减曲线
          data[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / length, 2.5) * 
            (1 + Math.sin(i / 4000) * 0.3)
        }
      }
      convolver.buffer = impulse

      return {
        nodes: { stereoPanner, delayLeft, delayRight, convolver, merger, splitter },
        connections: [
          [splitter, delayLeft, 0],
          [splitter, delayRight, 1],
          [delayLeft, merger, 0, 0],
          [delayRight, merger, 0, 1],
          [merger, convolver],
          [convolver, stereoPanner],
          [stereoPanner, context.destination]
        ]
      }
    }
  },
  {
    id: 'vocal',
    name: '清晰人声',
    description: '突出人声，减少其他乐器干扰',
    setup: (context: AudioContext) => {
      const vocalFilter = context.createBiquadFilter()
      const lowcutFilter = context.createBiquadFilter()
      const highcutFilter = context.createBiquadFilter()
      const presenceFilter = context.createBiquadFilter() // 新增一个滤波器
      const compressor = context.createDynamicsCompressor()

      // 优化人声频段
      vocalFilter.type = 'peaking'
      vocalFilter.frequency.value = 2500  // 调整到人声主要频段
      vocalFilter.Q.value = 1.5
      vocalFilter.gain.value = 5

      // 优化低频截止
      lowcutFilter.type = 'highpass'
      lowcutFilter.frequency.value = 150
      lowcutFilter.Q.value = 0.7

      // 优化高频控制
      highcutFilter.type = 'lowpass'
      highcutFilter.frequency.value = 8500
      highcutFilter.Q.value = 0.7

      // 添加存在感滤波器
      presenceFilter.type = 'peaking'
      presenceFilter.frequency.value = 5000
      presenceFilter.Q.value = 2
      presenceFilter.gain.value = 3

      // 优化压缩器设置
      compressor.threshold.value = -24
      compressor.knee.value = 12
      compressor.ratio.value = 3
      compressor.attack.value = 0.003
      compressor.release.value = 0.25

      return {
        nodes: {
          vocalFilter,
          lowcutFilter,
          highcutFilter,
          presenceFilter,
          compressor
        },
        connections: [
          [lowcutFilter, highcutFilter],
          [highcutFilter, vocalFilter],
          [vocalFilter, presenceFilter],
          [presenceFilter, compressor],
          [compressor, context.destination]
        ]
      }
    }
  },
  {
    id: 'live',
    name: '现场律动',
    description: '模拟现场演唱会的氛围感',
    setup: (context: AudioContext) => {
      const convolver = context.createConvolver()
      const bassFilter = context.createBiquadFilter()
      const presenceFilter = context.createBiquadFilter()
      const midFilter = context.createBiquadFilter() // 新增中频滤波器
      const compressor = context.createDynamicsCompressor()

      // 优化混响效果
      const reverbTime = 2.8
      const length = context.sampleRate * reverbTime
      const impulse = context.createBuffer(2, length, context.sampleRate)
      for (let channel = 0; channel < 2; channel++) {
        const data = impulse.getChannelData(channel)
        for (let i = 0; i < length; i++) {
          data[i] = ((Math.random() * 2) - 1) * 
            Math.pow(1 - i / length, 1.5) * 
            (1 + Math.sin(i / 5000) * 0.4)
        }
      }
      convolver.buffer = impulse

      // 优化低频
      bassFilter.type = 'lowshelf'
      bassFilter.frequency.value = 100
      bassFilter.gain.value = 4

      // 优化中频
      midFilter.type = 'peaking'
      midFilter.frequency.value = 800
      midFilter.Q.value = 1.2
      midFilter.gain.value = -2

      // 优化高频临场感
      presenceFilter.type = 'peaking'
      presenceFilter.frequency.value = 4500
      presenceFilter.Q.value = 1.5
      presenceFilter.gain.value = 3

      // 优化压缩器
      compressor.threshold.value = -22
      compressor.knee.value = 15
      compressor.ratio.value = 4
      compressor.attack.value = 0.02
      compressor.release.value = 0.2

      return {
        nodes: {
          convolver,
          bassFilter,
          midFilter,
          presenceFilter,
          compressor
        },
        connections: [
          [bassFilter, midFilter],
          [midFilter, presenceFilter],
          [presenceFilter, convolver],
          [convolver, compressor],
          [compressor, context.destination]
        ]
      }
    }
  },
  {
    id: 'speaker',
    name: '外放优化',
    description: '优化手机/音箱外放体验',
    setup: (context: AudioContext) => {
      const midFilter = context.createBiquadFilter()
      const bassFilter = context.createBiquadFilter()
      const presenceFilter = context.createBiquadFilter()
      const highFilter = context.createBiquadFilter() // 新增高频滤波器
      const compressor = context.createDynamicsCompressor()

      // 优化中频
      midFilter.type = 'peaking'
      midFilter.frequency.value = 1500
      midFilter.Q.value = 1.2
      midFilter.gain.value = 4

      // 优化低频
      bassFilter.type = 'lowshelf'
      bassFilter.frequency.value = 200
      bassFilter.gain.value = 5

      // 优化清晰度
      presenceFilter.type = 'peaking'
      presenceFilter.frequency.value = 3500
      presenceFilter.Q.value = 1.8
      presenceFilter.gain.value = 3

      // 控制高频
      highFilter.type = 'highshelf'
      highFilter.frequency.value = 7000
      highFilter.gain.value = -2

      // 优化压缩器
      compressor.threshold.value = -18
      compressor.knee.value = 20
      compressor.ratio.value = 5
      compressor.attack.value = 0.02
      compressor.release.value = 0.2

      return {
        nodes: {
          midFilter,
          bassFilter,
          presenceFilter,
          highFilter,
          compressor
        },
        connections: [
          [bassFilter, midFilter],
          [midFilter, presenceFilter],
          [presenceFilter, highFilter],
          [highFilter, compressor],
          [compressor, context.destination]
        ]
      }
    }
  }
]
